const app = getApp()
import { detail, close, receive, remove, code } from '../../../api/order'
import { create } from '../../../api/payment'
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog'

Page({
  data: {
    theme: '',
    back: true,
    title: '订单详细',
    loading: true,
    submitLoading: false,
    setting: {},
    detail: {},
    timeData: {},
    id: null,
    posterEncode: ''
  },
  onLoad (options) {
    this.data.id = options.id

    this.setData({
      theme: app.global.theme,
      setting: app.global.setting.order,
      loading: true
    })
  },
  onShow () {
    detail(this.data.id).then(res => {
      this.setData({
        detail: res.data
      })
    })
      .finally(() => {
        this.setData({ loading: false })
      })
  },
  onPullDownRefresh () { /* 监听用户下拉动作*/ },
  onReachBottom () { /* 页面上拉触底事件的处理函数*/ },

  // ===================method================//

  handleClose (event) {
    const that = this
    Dialog.confirm({
      message: '确定取消吗？'
    }).then(() => {
      this.setData({ submitLoading: true })
      const id = this.data.detail.id
      close(id).then(res => {
        app.toast('订单已关闭', function () {
          const detail = that.data.detail
          detail.order_status.value = -1
          detail.order_status.text = '已关闭'
          that.setData({
            detail: detail
          })
        }, 'success')
      })
        .finally(() => {
          this.setData({ submitLoading: false })
        })
    }).catch(() => { })
  },
  handleReceive (event) {
    this.setData({ submitLoading: true })
    const id = this.data.detail.id
    receive(id).then(res => {
      app.toast('订单签收成功', function () {
        app.redirect('/pages/order/comment/index?id=' + id)
      }, 'success')
    })
      .finally(() => {
        this.setData({ submitLoading: false })
      })
  },
  handleRemove (event) {
    Dialog.confirm({
      message: '确定删除吗？'
    }).then(() => {
      this.setData({ submitLoading: true })
      const id = this.data.detail.id
      remove(id).then(res => {
        app.toast('订单已删除', function () {
          wx.navigateBack({
            delta: 1
          })
        }, 'success')
      })
        .finally(() => {
          this.setData({ submitLoading: false })
        })
    }).catch(() => { })
  },
  handlePayment (event) {
    this.setData({ submitLoading: true })
    wx.showLoading({
      title: '支付中...'
    })
    const that = this
    const id = this.data.detail.id
    create(id).then(res => {
      wx.requestPayment({
        'timeStamp': res.data.timeStamp,
        'nonceStr': res.data.nonceStr,
        'package': res.data.package,
        'signType': res.data.signType,
        'paySign': res.data.paySign,
        'success': function (res) {
          app.toast('支付成功', function () {
            wx.navigateBack()
          }, 'success')
        },
        fail: function (res) {
          app.toast('您已取消本次支付')
        },
        complete: function (res) {
          that.setData({ submitLoading: false })
        }
      })
    })
      .finally(() => {
        wx.hideLoading()
      })
  },
  handleCode () {
    this.setData({ submitLoading: true })
    wx.showLoading({ title: '加载中...' })
    code(this.data.detail.id).then(res => {
      this.setData({
        posterEncode: res.data.encode,
        poster: true
      })
    })
      .finally(() => {
        this.setData({ submitLoading: false })
        wx.hideLoading()
      })
  },
  countDown (event) {
    this.setData({ timeData: event.detail })
  },
  countDownFinish () {
    const that = this
    app.toast('订单已关闭', function () {
      const detail = that.data.detail
      detail.order_status.value = -1
      detail.order_status.text = '已关闭'
      that.setData({
        detail: detail
      })
    }, 'success')
  },
  phoneCall (event) {
    wx.makePhoneCall({
      phoneNumber: event.currentTarget.dataset.phone
    })
  },
  redirect (event) {
    app.redirect('/pages/order/comment/index?id=' + this.data.detail.id)
  }
})